home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / readline.lha / readline / history.c < prev    next >
C/C++ Source or Header  |  1991-02-16  |  39KB  |  1,647 lines

  1. /* History.c -- standalone history library */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5.    This file contains the GNU History Library (the Library), a set of
  6.    routines for managing the text of previously typed lines.
  7.  
  8.    The Library is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 1, or (at your option)
  11.    any later version.
  12.  
  13.    The Library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22.  
  23. /* The goal is to make the implementation transparent, so that you
  24.    don't have to know what data types are used, just what functions
  25.    you can call.  I think I have done that. */
  26.  
  27. /* Remove these declarations when we have a complete libgnu.a. */
  28. #define STATIC_MALLOC
  29. #ifndef STATIC_MALLOC
  30. extern char *xmalloc (), *xrealloc ();
  31. #else
  32. static char *xmalloc (), *xrealloc ();
  33. #endif
  34.  
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <sys/file.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40.  
  41. #ifdef __GNUC__
  42. #define alloca __builtin_alloca
  43. #else
  44. #if defined (sparc) && defined (sun)
  45. #include <alloca.h>
  46. #else
  47. extern char *alloca ();
  48. #endif
  49. #endif
  50.  
  51. #include "history.h"
  52.  
  53. #ifndef savestring
  54. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  55. #endif
  56.  
  57. #ifndef whitespace
  58. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  59. #endif
  60.  
  61. #ifndef digit
  62. #define digit(c)  ((c) >= '0' && (c) <= '9')
  63. #endif
  64.  
  65. #ifndef member
  66. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  67. #endif
  68.  
  69. /* **************************************************************** */
  70. /*                                    */
  71. /*            History Functions                */
  72. /*                                    */
  73. /* **************************************************************** */
  74.  
  75. /* An array of HIST_ENTRY.  This is where we store the history. */
  76. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  77.  
  78. /* Non-zero means that we have enforced a limit on the amount of
  79.    history that we save. */
  80. int history_stifled = 0;
  81.  
  82. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  83.    entries to remember. */
  84. int max_input_history;
  85.  
  86. /* The current location of the interactive history pointer.  Just makes
  87.    life easier for outside callers. */
  88. static int history_offset = 0;
  89.  
  90. /* The number of strings currently stored in the input_history list. */
  91. int history_length = 0;
  92.  
  93. /* The current number of slots allocated to the input_history. */
  94. static int history_size = 0;
  95.  
  96. /* The number of slots to increase the_history by. */
  97. #define DEFAULT_HISTORY_GROW_SIZE 50
  98.  
  99. /* The character that represents the start of a history expansion
  100.    request.  This is usually `!'. */
  101. char history_expansion_char = '!';
  102.  
  103. /* The character that invokes word substitution if found at the start of
  104.    a line.  This is usually `^'. */
  105. char history_subst_char = '^';
  106.  
  107. /* During tokenization, if this character is seen as the first character
  108.    of a word, then it, and all subsequent characters upto a newline are
  109.    ignored.  For a Bourne shell, this should be '#'.  Bash special cases
  110.    the interactive comment character to not be a comment delimiter. */
  111. char history_comment_char = '\0';
  112.  
  113. /* The list of characters which inhibit the expansion of text if found
  114.    immediately following history_expansion_char. */
  115. char *history_no_expand_chars = " \t\n\r=";
  116.  
  117. /* The logical `base' of the history array.  It defaults to 1. */
  118. int history_base = 1;
  119.  
  120. /* Begin a session in which the history functions might be used.  This
  121.    initializes interactive variables. */
  122. void
  123. using_history ()
  124. {
  125.   history_offset = history_length;
  126. }
  127.  
  128. /* Return the number of bytes that the primary history entries are using.
  129.    This just adds up the lengths of the_history->lines. */
  130. int
  131. history_total_bytes ()
  132. {
  133.   register int i, result;
  134.  
  135.   for (i = 0; the_history && the_history[i]; i++)
  136.     result += strlen (the_history[i]->line);
  137.  
  138.   return (result);
  139. }
  140.  
  141. /* Place STRING at the end of the history list.  The data field
  142.    is  set to NULL. */
  143. void
  144. add_history (string)
  145.      char *string;
  146. {
  147.   HIST_ENTRY *temp;
  148.  
  149.   if (history_stifled && (history_length == max_input_history))
  150.     {
  151.       register int i;
  152.  
  153.       /* If the history is stifled, and history_length is zero,
  154.      and it equals max_input_history, we don't save items. */
  155.       if (!history_length)
  156.     return;
  157.  
  158.       /* If there is something in the slot, then remove it. */
  159.       if (the_history[0])
  160.     {
  161.       free (the_history[0]->line);
  162.       free (the_history[0]);
  163.     }
  164.  
  165.       for (i = 0; i < history_length; i++)
  166.     the_history[i] = the_history[i + 1];
  167.  
  168.       history_base++;
  169.  
  170.     }
  171.   else
  172.     {
  173.       if (!history_size)
  174.     {
  175.       the_history = (HIST_ENTRY **)
  176.         xmalloc ((history_size = DEFAULT_HISTORY_GROW_SIZE)
  177.              * sizeof (HIST_ENTRY *));
  178.       history_length = 1;
  179.  
  180.     }
  181.       else
  182.     {
  183.       if (history_length == (history_size - 1))
  184.         {
  185.           the_history = (HIST_ENTRY **)
  186.         xrealloc (the_history,
  187.               ((history_size += DEFAULT_HISTORY_GROW_SIZE)
  188.                * sizeof (HIST_ENTRY *)));
  189.       }
  190.       history_length++;
  191.     }
  192.     }
  193.  
  194.   temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  195.   temp->line = savestring (string);
  196.   temp->data = (char *)NULL;
  197.  
  198.   the_history[history_length] = (HIST_ENTRY *)NULL;
  199.   the_history[history_length - 1] = temp;
  200. }
  201.  
  202. /* Make the history entry at WHICH have LINE and DATA.  This returns
  203.    the old entry so you can dispose of the data.  In the case of an
  204.    invalid WHICH, a NULL pointer is returned. */
  205. HIST_ENTRY *
  206. replace_history_entry (which, line, data)
  207.      int which;
  208.      char *line;
  209.      char *data;
  210. {
  211.   HIST_ENTRY *temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  212.   HIST_ENTRY *old_value;
  213.  
  214.   if (which >= history_length)
  215.     return ((HIST_ENTRY *)NULL);
  216.  
  217.   old_value = the_history[which];
  218.  
  219.   temp->line = savestring (line);
  220.   temp->data = data;
  221.   the_history[which] = temp;
  222.  
  223.   return (old_value);
  224. }
  225.  
  226. /* Returns the magic number which says what history element we are
  227.    looking at now.  In this implementation, it returns history_offset. */
  228. int
  229. where_history ()
  230. {
  231.   return (history_offset);
  232. }
  233.  
  234. /* Search the history for STRING, starting at history_offset.
  235.    If DIRECTION < 0, then the search is through previous entries,
  236.    else through subsequent.  If the string is found, then
  237.    current_history () is the history entry, and the value of this function
  238.    is the offset in the line of that history entry that the string was
  239.    found in.  Otherwise, nothing is changed, and a -1 is returned. */
  240. int
  241. history_search (string, direction)
  242.      char *string;
  243.      int direction;
  244. {
  245.   register int i = history_offset;
  246.   register int reverse = (direction < 0);
  247.   register char *line;
  248.   register int index;
  249.   int string_len = strlen (string);
  250.  
  251.   /* Take care of trivial cases first. */
  252.  
  253.   if (!history_length || ((i == history_length) && !reverse))
  254.     return (-1);
  255.  
  256.   if (reverse && (i == history_length))
  257.     i--;
  258.  
  259.   while (1)
  260.     {
  261.       /* Search each line in the history list for STRING. */
  262.  
  263.       /* At limit for direction? */
  264.       if ((reverse && i < 0) ||
  265.       (!reverse && i == history_length))
  266.     return (-1);
  267.  
  268.       line = the_history[i]->line;
  269.       index = strlen (line);
  270.  
  271.       /* If STRING is longer than line, no match. */
  272.       if (string_len > index)
  273.     goto next_line;
  274.  
  275.       /* Do the actual search. */
  276.       if (reverse)
  277.     {
  278.       index -= string_len;
  279.  
  280.       while (index >= 0)
  281.         {
  282.           if (strncmp (string, line + index, string_len) == 0)
  283.         {
  284.           history_offset = i;
  285.           return (index);
  286.         }
  287.           index--;
  288.         }
  289.     }
  290.       else
  291.     {
  292.       register int limit = (string_len - index) + 1;
  293.       index = 0;
  294.  
  295.       while (index < limit)
  296.         {
  297.           if (strncmp (string, line + index, string_len) == 0)
  298.         {
  299.           history_offset = i;
  300.           return (index);
  301.         }
  302.           index++;
  303.         }
  304.     }
  305.     next_line:
  306.       if (reverse)
  307.     i--;
  308.       else
  309.     i++;
  310.     }
  311. }
  312.  
  313. /* Remove history element WHICH from the history.  The removed
  314.    element is returned to you so you can free the line, data,
  315.    and containing structure. */
  316. HIST_ENTRY *
  317. remove_history (which)
  318.      int which;
  319. {
  320.   HIST_ENTRY *return_value;
  321.  
  322.   if (which >= history_length || !history_length)
  323.     return_value = (HIST_ENTRY *)NULL;
  324.   else
  325.     {
  326.       register int i;
  327.       return_value = the_history[which];
  328.  
  329.       for (i = which; i < history_length; i++)
  330.     the_history[i] = the_history[i + 1];
  331.  
  332.       history_length--;
  333.     }
  334.  
  335.   return (return_value);
  336. }
  337.  
  338. /* Stifle the history list, remembering only MAX number of lines. */
  339. void
  340. stifle_history (max)
  341.      int max;
  342. {
  343.   if (history_length > max)
  344.     {
  345.       register int i, j;
  346.  
  347.       /* This loses because we cannot free the data. */
  348.       for (i = 0; i < (history_length - max); i++)
  349.     {
  350.       free (the_history[i]->line);
  351.       free (the_history[i]);
  352.     }
  353.       history_base = i;
  354.       for (j = 0, i = history_length - max; j < max; i++, j++)
  355.     the_history[j] = the_history[i];
  356.       the_history[j] = (HIST_ENTRY *)NULL;
  357.       history_length = j;
  358.     }
  359.   history_stifled = 1;
  360.   max_input_history = max;
  361. }
  362.  
  363. /* Stop stifling the history.  This returns the previous amount the history
  364.  was stifled by.  The value is positive if the history was stifled, negative
  365.  if it wasn't. */
  366. int
  367. unstifle_history ()
  368. {
  369.   int result = max_input_history;
  370.   if (history_stifled)
  371.     {
  372.       result = - result;
  373.       history_stifled = 0;
  374.     }
  375.   return (result);
  376. }
  377.  
  378. /* Return the string that should be used in the place of this
  379.    filename.  This only matters when you don't specify the
  380.    filename to read_history (), or write_history (). */
  381. static char *
  382. history_filename (filename)
  383.      char *filename;
  384. {
  385.   char *return_val = filename ? savestring (filename) : (char *)NULL;
  386.  
  387.   if (!return_val)
  388.     {
  389.       char *home = (char *)getenv ("HOME");
  390.       if (!home) home = ".";
  391.       return_val = (char *)xmalloc (2 + strlen (home) + strlen (".history"));
  392.       sprintf (return_val, "%s/.history", home);
  393.     }
  394.   return (return_val);
  395. }
  396.  
  397. /* Add the contents of FILENAME to the history list, a line at a time.
  398.    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
  399.    successful, or errno if not. */
  400. int
  401. read_history (filename)
  402.      char *filename;
  403. {
  404.   return (read_history_range (filename, 0, -1));
  405. }
  406.  
  407. /* Read a range of lines from FILENAME, adding them to the history list.
  408.    Start reading at the FROM'th line and end at the TO'th.  If FROM
  409.    is zero, start at the beginning.  If TO is less than FROM, read
  410.    until the end of the file.  If FILENAME is NULL, then read from
  411.    ~/.history.  Returns 0 if successful, or errno if not. */
  412. int
  413. read_history_range (filename, from, to)
  414.      char *filename;
  415.      int from, to;
  416. {
  417.   register int line_start, line_end;
  418.   char *input, *buffer = (char *)NULL;
  419.   int file, current_line, done;
  420.   struct stat finfo;
  421.   extern int errno;
  422.  
  423.   input = history_filename (filename);
  424.   file = open (input, O_RDONLY, 0666);
  425.  
  426.   if ((file < 0) ||
  427.       (stat (input, &finfo) == -1))
  428.     goto error_and_exit;
  429.  
  430.   buffer = (char *)xmalloc (finfo.st_size + 1);
  431.  
  432.   if (read (file, buffer, finfo.st_size) != finfo.st_size)
  433.   error_and_exit:
  434.     {
  435.       if (file >= 0)
  436.     close (file);
  437.  
  438.       if (buffer)
  439.     free (buffer);
  440.  
  441.       return (errno);
  442.     }
  443.  
  444.   close (file);
  445.  
  446.   /* Set TO to larger than end of file if negative. */
  447.   if (to < 0)
  448.     to = finfo.st_size;
  449.  
  450.   /* Start at beginning of file, work to end. */
  451.   line_start = line_end = current_line = 0;
  452.  
  453.   /* Skip lines until we are at FROM. */
  454.   while (line_start < finfo.st_size && current_line < from)
  455.     {
  456.       for (line_end = line_start; line_end < finfo.st_size; line_end++)
  457.     if (buffer[line_end] == '\n')
  458.       {
  459.         current_line++;
  460.         line_start = line_end + 1;
  461.         if (current_line == from)
  462.           break;
  463.       }
  464.     }
  465.  
  466.   /* If there are lines left to gobble, then gobble them now. */
  467.   for (line_end = line_start; line_end < finfo.st_size; line_end++)
  468.     if (buffer[line_end] == '\n')
  469.       {
  470.     buffer[line_end] = '\0';
  471.  
  472.     if (buffer[line_start])
  473.       add_history (buffer + line_start);
  474.  
  475.     current_line++;
  476.  
  477.     if (current_line >= to)
  478.       break;
  479.  
  480.     line_start = line_end + 1;
  481.       }
  482.   return (0);
  483. }
  484.  
  485. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  486.    If FNAME is NULL, then use ~/.history. */
  487. history_truncate_file (fname, lines)
  488.      char *fname;
  489.      register int lines;
  490. {
  491.   register int i;
  492.   int file;
  493.   char *buffer = (char *)NULL, *filename;
  494.   struct stat finfo;
  495.  
  496.   filename = history_filename (fname);
  497.   if (stat (filename, &finfo) == -1)
  498.     goto truncate_exit;
  499.  
  500.   file = open (filename, O_RDONLY, 066);
  501.  
  502.   if (file == -1)
  503.     goto truncate_exit;
  504.  
  505.   buffer = (char *)xmalloc (finfo.st_size + 1);
  506.   read (file, buffer, finfo.st_size);
  507.   close (file);
  508.  
  509.   /* Count backwards from the end of buffer until we have passed
  510.      LINES lines. */
  511.   for (i = finfo.st_size; lines && i; i--)
  512.     {
  513.       if (buffer[i] == '\n')
  514.     lines--;
  515.     }
  516.  
  517.   /* If there are fewer lines in the file than we want to truncate to,
  518.      then we are all done. */
  519.   if (!i)
  520.     goto truncate_exit;
  521.  
  522.   /* Otherwise, write from the start of this line until the end of the
  523.      buffer. */
  524.   for (--i; i; i--)
  525.     if (buffer[i] == '\n')
  526.       {
  527.     i++;
  528.     break;
  529.       }
  530.  
  531.   file = open (filename, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  532.   if (file == -1)
  533.     goto truncate_exit;
  534.  
  535.   write (file, buffer + i, finfo.st_size - i);
  536.   close (file);
  537.  
  538.  truncate_exit:
  539.   if (buffer)
  540.     free (buffer);
  541.  
  542.   free (filename);
  543. }
  544.  
  545. #define HISTORY_APPEND 0
  546. #define HISTORY_OVERWRITE 1
  547.  
  548. /* Workhorse function for writing history.  Writes NELEMENT entries
  549.    from the history list to FILENAME.  OVERWRITE is non-zero if you
  550.    wish to replace FILENAME with the entries. */
  551. static int
  552. history_do_write (filename, nelements, overwrite)
  553.      char *filename;
  554.      int nelements, overwrite;
  555. {
  556.   extern int errno;
  557.   register int i;
  558.   char *output = history_filename (filename);
  559.   int file, mode;
  560.   char cr = '\n';
  561.  
  562.   if (overwrite)
  563.     mode = O_WRONLY | O_CREAT | O_TRUNC;
  564.   else
  565.     mode = O_WRONLY | O_APPEND;
  566.  
  567.   if ((file = open (output, mode, 0666)) == -1)
  568.     return (errno);
  569.  
  570.   if (nelements > history_length)
  571.     nelements = history_length;
  572.  
  573.   for (i = history_length - nelements; i < history_length; i++)
  574.     {
  575.       if (write (file, the_history[i]->line, strlen (the_history[i]->line)) < 0)
  576.     break;
  577.       if (write (file, &cr, 1) < 0)
  578.     break;
  579.     }
  580.  
  581.   close (file);
  582.   return (0);
  583. }
  584.   
  585. /* Append NELEMENT entries to FILENAME.  The entries appended are from
  586.    the end of the list minus NELEMENTs up to the end of the list. */
  587. int
  588. append_history (nelements, filename)
  589.      int nelements;
  590.      char *filename;
  591. {
  592.   return (history_do_write (filename, nelements, HISTORY_APPEND));
  593. }
  594.  
  595. /* Overwrite FILENAME with the current history.  If FILENAME is NULL,
  596.    then write the history list to ~/.history.  Values returned
  597.    are as in read_history ().*/
  598. int
  599. write_history (filename)
  600.      char *filename;
  601. {
  602.   return (history_do_write (filename, history_length, HISTORY_OVERWRITE));
  603. }
  604.  
  605. /* Return the history entry at the current position, as determined by
  606.    history_offset.  If there is no entry there, return a NULL pointer. */
  607. HIST_ENTRY *
  608. current_history ()
  609. {
  610.   if ((history_offset == history_length) || !the_history)
  611.     return ((HIST_ENTRY *)NULL);
  612.   else
  613.     return (the_history[history_offset]);
  614. }
  615.  
  616. /* Back up history_offset to the previous history entry, and return
  617.    a pointer to that entry.  If there is no previous entry then return
  618.    a NULL pointer. */
  619. HIST_ENTRY *
  620. previous_history ()
  621. {
  622.   if (!history_offset)
  623.     return ((HIST_ENTRY *)NULL);
  624.   else
  625.     return (the_history[--history_offset]);
  626. }
  627.  
  628. /* Move history_offset forward to the next history entry, and return
  629.    a pointer to that entry.  If there is no next entry then return a
  630.    NULL pointer. */
  631. HIST_ENTRY *
  632. next_history ()
  633. {
  634.   if (history_offset == history_length)
  635.     return ((HIST_ENTRY *)NULL);
  636.   else
  637.     return (the_history[++history_offset]);
  638. }
  639.  
  640. /* Return the current history array.  The caller has to be carefull, since this
  641.    is the actual array of data, and could be bashed or made corrupt easily.
  642.    The array is terminated with a NULL pointer. */
  643. HIST_ENTRY **
  644. history_list ()
  645. {
  646.   return (the_history);
  647. }
  648.  
  649. /* Return the history entry which is logically at OFFSET in the history array.
  650.    OFFSET is relative to history_base. */
  651. HIST_ENTRY *
  652. history_get (offset)
  653.      int offset;
  654. {
  655.   int index = offset - history_base;
  656.  
  657.   if (index >= history_length ||
  658.       index < 0 ||
  659.       !the_history)
  660.     return ((HIST_ENTRY *)NULL);
  661.   return (the_history[index]);
  662. }
  663.  
  664. /* Search for STRING in the history list.  DIR is < 0 for searching
  665.    backwards.  POS is an absolute index into the history list at
  666.    which point to begin searching. */
  667. int
  668. history_search_pos (string, dir, pos)
  669.      char *string;
  670.      int dir, pos;
  671. {
  672.   int ret, old = where_history ();
  673.   history_set_pos (pos);
  674.   if (history_search (string, dir) == -1)
  675.     {
  676.       history_set_pos (old);
  677.       return (-1);
  678.     }
  679.   ret = where_history ();
  680.   history_set_pos (old);
  681.   return ret;
  682. }
  683.  
  684. /* Make the current history item be the one at POS, an absolute index.
  685.    Returns zero if POS is out of range, else non-zero. */
  686. int
  687. history_set_pos (pos)
  688.      int pos;
  689. {
  690.   if (pos > history_length || pos < 0 || !the_history)
  691.     return (0);
  692.   history_offset = pos;
  693.   return (1);
  694. }
  695.  
  696.  
  697. /* **************************************************************** */
  698. /*                                    */
  699. /*            History Expansion                */
  700. /*                                    */
  701. /* **************************************************************** */
  702.  
  703. /* Hairy history expansion on text, not tokens.  This is of general
  704.    use, and thus belongs in this library. */
  705.  
  706. /* The last string searched for in a !?string? search. */
  707. static char *search_string = (char *)NULL;
  708.  
  709. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  710.    point to after the event specifier.  Just a pointer to the history
  711.    line is returned; NULL is returned in the event of a bad specifier.
  712.    You pass STRING with *INDEX equal to the history_expansion_char that
  713.    begins this specification.
  714.    DELIMITING_QUOTE is a character that is allowed to end the string
  715.    specification for what to search for in addition to the normal
  716.    characters `:', ` ', `\t', `\n', and sometimes `?'.
  717.    So you might call this function like:
  718.    line = get_history_event ("!echo:p", &index, 0);  */
  719. char *
  720. get_history_event (string, caller_index, delimiting_quote)
  721.      char *string;
  722.      int *caller_index;
  723.      int delimiting_quote;
  724. {
  725.   register int i = *caller_index;
  726.   int which, sign = 1;
  727.   HIST_ENTRY *entry;
  728.  
  729.   /* The event can be specified in a number of ways.
  730.  
  731.      !!   the previous command
  732.      !n   command line N
  733.      !-n  current command-line minus N
  734.      !str the most recent command starting with STR
  735.      !?str[?]
  736.       the most recent command containing STR
  737.  
  738.      All values N are determined via HISTORY_BASE. */
  739.  
  740.   if (string[i] != history_expansion_char)
  741.     return ((char *)NULL);
  742.  
  743.   /* Move on to the specification. */
  744.   i++;
  745.  
  746.   /* Handle !! case. */
  747.   if (string[i] == history_expansion_char)
  748.     {
  749.       i++;
  750.       which = history_base + (history_length - 1);
  751.       *caller_index = i;
  752.       goto get_which;
  753.     }
  754.  
  755.   /* Hack case of numeric line specification. */
  756.  read_which:
  757.   if (string[i] == '-')
  758.     {
  759.       sign = -1;
  760.       i++;
  761.     }
  762.  
  763.   if (digit (string[i]))
  764.     {
  765.       int start = i;
  766.  
  767.       /* Get the extent of the digits. */
  768.       for (; digit (string[i]); i++);
  769.  
  770.       /* Get the digit value. */
  771.       sscanf (string + start, "%d", &which);
  772.  
  773.       *caller_index = i;
  774.  
  775.       if (sign < 0)
  776.     which = (history_length + history_base) - which;
  777.  
  778.     get_which:
  779.       if (entry = history_get (which))
  780.     return (entry->line);
  781.  
  782.       return ((char *)NULL);
  783.     }
  784.  
  785.   /* This must be something to search for.  If the spec begins with
  786.      a '?', then the string may be anywhere on the line.  Otherwise,
  787.      the string must be found at the start of a line. */
  788.   {
  789.     int index;
  790.     char *temp;
  791.     int substring_okay = 0;
  792.  
  793.     if (string[i] == '?')
  794.       {
  795.     substring_okay++;
  796.     i++;
  797.       }
  798.  
  799.     for (index = i; string[i]; i++)
  800.       if (whitespace (string[i]) ||
  801.       string[i] == '\n' ||
  802.       string[i] == ':' ||
  803.       (substring_okay && string[i] == '?') ||
  804.       string[i] == delimiting_quote)
  805.     break;
  806.  
  807.     temp = (char *)alloca (1 + (i - index));
  808.     strncpy (temp, &string[index], (i - index));
  809.     temp[i - index] = '\0';
  810.  
  811.     if (string[i] == '?')
  812.       i++;
  813.  
  814.     *caller_index = i;
  815.  
  816.   search_again:
  817.  
  818.     index = history_search (temp, -1);
  819.  
  820.     if (index < 0)
  821.     search_lost:
  822.       {
  823.     history_offset = history_length;
  824.     return ((char *)NULL);
  825.       }
  826.  
  827.     if (index == 0 || substring_okay || 
  828.     (strncmp (temp, the_history[history_offset]->line,
  829.           strlen (temp)) == 0))
  830.       {
  831.       search_won:
  832.     entry = current_history ();
  833.     history_offset = history_length;
  834.     
  835.     /* If this was a substring search, then remember the string that
  836.        we matched for word substitution. */
  837.     if (substring_okay)
  838.       {
  839.         if (search_string)
  840.           free (search_string);
  841.         search_string = savestring (temp);
  842.       }
  843.  
  844.     return (entry->line);
  845.       }
  846.  
  847.     if (history_offset)
  848.       history_offset--;
  849.     else
  850.       goto search_lost;
  851.     
  852.     goto search_again;
  853.   }
  854. }
  855.  
  856. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  857.    to a string.  Returns:
  858.  
  859.    0) If no expansions took place (or, if the only change in
  860.       the text was the de-slashifying of the history expansion
  861.       character)
  862.    1) If expansions did take place
  863.   -1) If there was an error in expansion.
  864.  
  865.   If an error ocurred in expansion, then OUTPUT contains a descriptive
  866.   error message. */
  867. int
  868. history_expand (string, output)
  869.      char *string;
  870.      char **output;
  871. {
  872.   register int j, l = strlen (string);
  873.   int i, word_spec_error = 0;
  874.   int cc, modified = 0;
  875.   char *word_spec, *event;
  876.   int starting_index, only_printing = 0, substitute_globally = 0;
  877.  
  878.   char *get_history_word_specifier (), *rindex ();
  879.  
  880.   /* The output string, and its length. */
  881.   int len = 0;
  882.   char *result = (char *)NULL;
  883.  
  884.   /* Used in add_string; */
  885.   char *temp, tt[2], tbl[3];
  886.  
  887.   /* Prepare the buffer for printing error messages. */
  888.   result = (char *)xmalloc (len = 255);
  889.  
  890.   result[0] = tt[1] = tbl[2] = '\0';
  891.   tbl[0] = '\\';
  892.   tbl[1] = history_expansion_char;
  893.  
  894.   /* Grovel the string.  Only backslash can quote the history escape
  895.      character.  We also handle arg specifiers. */
  896.  
  897.   /* Before we grovel forever, see if the history_expansion_char appears
  898.      anywhere within the text. */
  899.  
  900.   /* The quick substitution character is a history expansion all right.  That
  901.      is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  902.      that is the substitution that we do. */
  903.   if (string[0] == history_subst_char)
  904.     {
  905.       char *format_string = (char *)alloca (10 + strlen (string));
  906.  
  907.       sprintf (format_string, "%c%c:s%s",
  908.            history_expansion_char, history_expansion_char,
  909.            string);
  910.       string = format_string;
  911.       l += 4;
  912.       goto grovel;
  913.     }
  914.  
  915.   /* If not quick substitution, still maybe have to do expansion. */
  916.  
  917.   /* `!' followed by one of the characters in history_no_expand_chars
  918.      is NOT an expansion. */
  919.   for (i = 0; string[i]; i++)
  920.     if (string[i] == history_expansion_char)
  921.       if (!string[i + 1] || member (string[i + 1], history_no_expand_chars))
  922.     continue;
  923.       else
  924.     goto grovel;
  925.  
  926.   free (result);
  927.   *output = savestring (string);
  928.   return (0);
  929.  
  930.  grovel:
  931.  
  932.   for (i = j = 0; i < l; i++)
  933.     {
  934.       int tchar = string[i];
  935.       if (tchar == history_expansion_char)
  936.     tchar = -3;
  937.  
  938.       switch (tchar)
  939.     {
  940.     case '\\':
  941.       if (string[i + 1] == history_expansion_char)
  942.         {
  943.           i++;
  944.           temp = tbl;
  945.           goto do_add;
  946.         }
  947.       else
  948.         goto add_char;
  949.  
  950.       /* case history_expansion_char: */
  951.     case -3:
  952.       starting_index = i + 1;
  953.       cc = string[i + 1];
  954.  
  955.       /* If the history_expansion_char is followed by one of the
  956.          characters in history_no_expand_chars, then it is not a
  957.          candidate for expansion of any kind. */
  958.       if (member (cc, history_no_expand_chars))
  959.         goto add_char;
  960.  
  961.       /* There is something that is listed as a `word specifier' in csh
  962.          documentation which means `the expanded text to this point'.
  963.          That is not a word specifier, it is an event specifier. */
  964.  
  965.       if (cc == '#')
  966.         goto hack_pound_sign;
  967.  
  968.       /* If it is followed by something that starts a word specifier,
  969.          then !! is implied as the event specifier. */
  970.  
  971.       if (member (cc, ":$*%^"))
  972.         {
  973.           char fake_s[3];
  974.           int fake_i = 0;
  975.           i++;
  976.           fake_s[0] = fake_s[1] = history_expansion_char;
  977.           fake_s[2] = '\0';
  978.           event = get_history_event (fake_s, &fake_i, 0);
  979.         }
  980.       else
  981.         {
  982.           int quoted_search_delimiter = 0;
  983.  
  984.           /* If the character before this `!' is a double or single
  985.          quote, then this expansion takes place inside of the
  986.          quoted string.  If we have to search for some text ("!foo"),
  987.          allow the delimiter to end the search string. */
  988.           if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  989.         quoted_search_delimiter = string[i - 1];
  990.  
  991.           event = get_history_event (string, &i, quoted_search_delimiter);
  992.         }
  993.       
  994.       if (!event)
  995.       event_not_found:
  996.         {
  997.         int l = 1 + (i - starting_index);
  998.  
  999.         temp = (char *)alloca (1 + l);
  1000.         strncpy (temp, string + starting_index, l);
  1001.         temp[l - 1] = 0;
  1002.         sprintf (result, "%s: %s.", temp,
  1003.              word_spec_error ? "Bad word specifier" : "Event not found");
  1004.       error_exit:
  1005.         *output = result;
  1006.         return (-1);
  1007.       }
  1008.  
  1009.       /* If a word specifier is found, then do what that requires. */
  1010.       starting_index = i;
  1011.  
  1012.       word_spec = get_history_word_specifier (string, event, &i);
  1013.  
  1014.       /* There is no such thing as a `malformed word specifier'.  However,
  1015.          it is possible for a specifier that has no match.  In that case,
  1016.          we complain. */
  1017.       if (word_spec == (char *)-1)
  1018.       bad_word_spec:
  1019.       {
  1020.         word_spec_error++;
  1021.         goto event_not_found;
  1022.       }
  1023.  
  1024.       /* If no word specifier, than the thing of interest was the event. */
  1025.       if (!word_spec)
  1026.         temp = event;
  1027.       else
  1028.         {
  1029.           temp = (char *)alloca (1 + strlen (word_spec));
  1030.           strcpy (temp, word_spec);
  1031.           free (word_spec);
  1032.         }
  1033.  
  1034.       /* Perhaps there are other modifiers involved.  Do what they say. */
  1035.  
  1036.     hack_specials:
  1037.  
  1038.       if (string[i] == ':')
  1039.         {
  1040.           char *tstr;
  1041.  
  1042.           switch (string[i + 1])
  1043.         {
  1044.           /* :p means make this the last executed line.  So we
  1045.              return an error state after adding this line to the
  1046.              history. */
  1047.         case 'p':
  1048.           only_printing++;
  1049.           goto next_special;
  1050.  
  1051.           /* :t discards all but the last part of the pathname. */
  1052.         case 't':
  1053.           tstr = rindex (temp, '/');
  1054.           if (tstr)
  1055.             temp = ++tstr;
  1056.           goto next_special;
  1057.  
  1058.           /* :h discards the last part of a pathname. */
  1059.         case 'h':
  1060.           tstr = rindex (temp, '/');
  1061.           if (tstr)
  1062.             *tstr = '\0';
  1063.           goto next_special;
  1064.  
  1065.           /* :r discards the suffix. */
  1066.         case 'r':
  1067.           tstr = rindex (temp, '.');
  1068.           if (tstr)
  1069.             *tstr = '\0';
  1070.           goto next_special;
  1071.  
  1072.           /* :e discards everything but the suffix. */
  1073.         case 'e':
  1074.           tstr = rindex (temp, '.');
  1075.           if (tstr)
  1076.             temp = tstr;
  1077.           goto next_special;
  1078.  
  1079.           /* :s/this/that substitutes `this' for `that'. */
  1080.           /* :gs/this/that substitutes `this' for `that' globally. */
  1081.         case 'g':
  1082.           if (string[i + 2] == 's')
  1083.             {
  1084.               i++;
  1085.               substitute_globally = 1;
  1086.               goto substitute;
  1087.             }
  1088.           else
  1089.             
  1090.         case 's':
  1091.           substitute:
  1092.           {
  1093.             char *this, *that, *new_event;
  1094.             int delimiter = 0;
  1095.             int si, l_this, l_that, l_temp = strlen (temp);
  1096.  
  1097.             if (i + 2 < strlen (string))
  1098.               delimiter = string[i + 2];
  1099.  
  1100.             if (!delimiter)
  1101.               break;
  1102.  
  1103.             i += 3;
  1104.  
  1105.             /* Get THIS. */
  1106.             for (si = i; string[si] && string[si] != delimiter; si++);
  1107.             l_this = (si - i);
  1108.             this = (char *)alloca (1 + l_this);
  1109.             strncpy (this, string + i, l_this);
  1110.             this[l_this] = '\0';
  1111.  
  1112.             i = si;
  1113.             if (string[si])
  1114.               i++;
  1115.  
  1116.             /* Get THAT. */
  1117.             for (si = i; string[si] && string[si] != delimiter; si++);
  1118.             l_that = (si - i);
  1119.             that = (char *)alloca (1 + l_that);
  1120.             strncpy (that, string + i, l_that);
  1121.             that[l_that] = '\0';
  1122.  
  1123.             i = si;
  1124.             if (string[si]) i++;
  1125.  
  1126.             /* Ignore impossible cases. */
  1127.             if (l_this > l_temp)
  1128.               goto cant_substitute;
  1129.  
  1130.             /* Find the first occurrence of THIS in TEMP. */
  1131.             si = 0;
  1132.             for (; (si + l_this) <= l_temp; si++)
  1133.               if (strncmp (temp + si, this, l_this) == 0)
  1134.             {
  1135.               new_event =
  1136.                 (char *)alloca (1 + (l_that - l_this) + l_temp);
  1137.               strncpy (new_event, temp, si);
  1138.               strncpy (new_event + si, that, l_that);
  1139.               strncpy (new_event + si + l_that,
  1140.                    temp + si + l_this,
  1141.                    l_temp - (si + l_this));
  1142.               new_event[(l_that - l_this) + l_temp] = '\0';
  1143.               temp = new_event;
  1144.  
  1145.               if (substitute_globally)
  1146.                 {
  1147.                   si += l_that;
  1148.                   l_temp = strlen (temp);
  1149.                   substitute_globally++;
  1150.                   continue;
  1151.                 }
  1152.  
  1153.               goto hack_specials;
  1154.             }
  1155.  
  1156.           cant_substitute:
  1157.  
  1158.             if (substitute_globally > 1)
  1159.               {
  1160.             substitute_globally = 0;
  1161.             goto hack_specials;
  1162.               }
  1163.  
  1164.             goto event_not_found;
  1165.           }
  1166.  
  1167.           /* :# is the line so far.  Note that we have to
  1168.              alloca () it since RESULT could be realloc ()'ed
  1169.              below in add_string. */
  1170.         case '#':
  1171.         hack_pound_sign:
  1172.           if (result)
  1173.             {
  1174.               temp = (char *)alloca (1 + strlen (result));
  1175.               strcpy (temp, result);
  1176.             }
  1177.           else
  1178.             temp = "";
  1179.  
  1180.         next_special:
  1181.           i += 2;
  1182.           goto hack_specials;
  1183.         }
  1184.  
  1185.         }
  1186.       /* Believe it or not, we have to back the pointer up by one. */
  1187.       --i;
  1188.       goto add_string;
  1189.  
  1190.       /* A regular character.  Just add it to the output string. */
  1191.     default:
  1192.     add_char:
  1193.       tt[0] = string[i];
  1194.       temp = tt;
  1195.       goto do_add;
  1196.  
  1197.     add_string:
  1198.       modified++;
  1199.  
  1200.     do_add:
  1201.       j += strlen (temp);
  1202.       while (j > len)
  1203.         result = (char *)xrealloc (result, (len += 255));
  1204.  
  1205.       strcpy (result + (j - strlen (temp)), temp);
  1206.     }
  1207.     }
  1208.  
  1209.   *output = result;
  1210.  
  1211.   if (only_printing)
  1212.     {
  1213.       add_history (result);
  1214.       return (-1);
  1215.     }
  1216.  
  1217.   return (modified != 0);
  1218. }
  1219.  
  1220. /* Return a consed string which is the word specified in SPEC, and found
  1221.    in FROM.  NULL is returned if there is no spec.  -1 is returned if
  1222.    the word specified cannot be found.  CALLER_INDEX is the offset in
  1223.    SPEC to start looking; it is updated to point to just after the last
  1224.    character parsed. */
  1225. char *
  1226. get_history_word_specifier (spec, from, caller_index)
  1227.      char *spec, *from;
  1228.      int *caller_index;
  1229. {
  1230.   register int i = *caller_index;
  1231.   int first, last;
  1232.   int expecting_word_spec = 0;
  1233.   char *history_arg_extract ();
  1234.  
  1235.   /* The range of words to return doesn't exist yet. */
  1236.   first = last = 0;
  1237.  
  1238.   /* If we found a colon, then this *must* be a word specification.  If
  1239.      it isn't, then it is an error. */
  1240.   if (spec[i] == ':')
  1241.     i++, expecting_word_spec++;
  1242.  
  1243.   /* Handle special cases first. */
  1244.  
  1245.   /* `%' is the word last searched for. */
  1246.   if (spec[i] == '%')
  1247.     {
  1248.       *caller_index = i + 1;
  1249.       if (search_string)
  1250.     return (savestring (search_string));
  1251.       else
  1252.     return (savestring (""));
  1253.     }
  1254.  
  1255.   /* `*' matches all of the arguments, but not the command. */
  1256.   if (spec[i] == '*')
  1257.     {
  1258.       char *star_result;
  1259.  
  1260.       *caller_index = i + 1;
  1261.       star_result = history_arg_extract (1, '$', from);
  1262.  
  1263.       if (!star_result)
  1264.     star_result = savestring ("");
  1265.  
  1266.       return (star_result);
  1267.     }
  1268.  
  1269.   /* `$' is last arg. */
  1270.   if (spec[i] == '$')
  1271.     {
  1272.       *caller_index = i + 1;
  1273.       return (history_arg_extract ('$', '$', from));
  1274.     }
  1275.  
  1276.   /* Try to get FIRST and LAST figured out. */
  1277.   if (spec[i] == '-' || spec[i] == '^')
  1278.     {
  1279.       first = 1;
  1280.       goto get_last;
  1281.     }
  1282.  
  1283.  get_first:
  1284.   if (digit (spec[i]) && expecting_word_spec)
  1285.     {
  1286.       sscanf (spec + i, "%d", &first);
  1287.       for (; digit (spec[i]); i++);
  1288.     }
  1289.   else
  1290.     return ((char *)NULL);
  1291.  
  1292.  get_last:
  1293.   if (spec[i] == '^')
  1294.     {
  1295.       i++;
  1296.       last = 1;
  1297.       goto get_args;
  1298.     }
  1299.  
  1300.   if (spec[i] != '-')
  1301.     {
  1302.       last = first;
  1303.       goto get_args;
  1304.     }
  1305.  
  1306.   i++;
  1307.  
  1308.   if (digit (spec[i]))
  1309.     {
  1310.       sscanf (spec + i, "%d", &last);
  1311.       for (; digit (spec[i]); i++);
  1312.     }
  1313.   else
  1314.     if (spec[i] == '$')
  1315.       {
  1316.     i++;
  1317.     last = '$';
  1318.       }
  1319.  
  1320.  get_args:
  1321.   {
  1322.     char *result = (char *)NULL;
  1323.  
  1324.     *caller_index = i;
  1325.  
  1326.     if (last >= first)
  1327.       result = history_arg_extract (first, last, from);
  1328.  
  1329.     if (result)
  1330.       return (result);
  1331.     else
  1332.       return ((char *)-1);
  1333.   }
  1334. }
  1335.  
  1336. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1337.    The args are taken from STRING.  If either FIRST or LAST is < 0,
  1338.    then make that arg count from the right (subtract from the number of
  1339.    tokens, so that FIRST = -1 means the next to last token on the line). */
  1340. char *
  1341. history_arg_extract (first, last, string)
  1342.      int first, last;
  1343.      char *string;
  1344. {
  1345.   register int i, len;
  1346.   char *result = (char *)NULL;
  1347.   int size = 0, offset = 0;
  1348.  
  1349.   char **history_tokenize (), **list;
  1350.  
  1351.   if (!(list = history_tokenize (string)))
  1352.     return ((char *)NULL);
  1353.  
  1354.   for (len = 0; list[len]; len++);
  1355.  
  1356.   if (last < 0)
  1357.     last = len + last - 1;
  1358.  
  1359.   if (first < 0)
  1360.     first = len + first - 1;
  1361.  
  1362.   if (last == '$')
  1363.     last = len - 1;
  1364.  
  1365.   if (first == '$')
  1366.     first = len - 1;
  1367.  
  1368.   last++;
  1369.  
  1370.   if (first > len || last > len || first < 0 || last < 0)
  1371.     result = ((char *)NULL);
  1372.   else
  1373.     {
  1374.       for (i = first; i < last; i++)
  1375.     {
  1376.       int l = strlen (list[i]);
  1377.  
  1378.       if (!result)
  1379.         result = (char *)xmalloc ((size = (2 + l)));
  1380.       else
  1381.         result = (char *)xrealloc (result, (size += (2 + l)));
  1382.       strcpy (result + offset, list[i]);
  1383.       offset += l;
  1384.       if (i + 1 < last)
  1385.         {
  1386.           strcpy (result + offset, " ");
  1387.           offset++;
  1388.         }
  1389.     }
  1390.     }
  1391.  
  1392.   for (i = 0; i < len; i++)
  1393.     free (list[i]);
  1394.  
  1395.   free (list);
  1396.  
  1397.   return (result);
  1398. }
  1399.  
  1400. #define slashify_in_quotes "\\`\"$"
  1401.  
  1402. /* Return an array of tokens, much as the shell might.  The tokens are
  1403.    parsed out of STRING. */
  1404. char **
  1405. history_tokenize (string)
  1406.      char *string;
  1407. {
  1408.   char **result = (char **)NULL;
  1409.   register int i, start, result_index, size;
  1410.   int len;
  1411.  
  1412.   i = result_index = size = 0;
  1413.  
  1414.   /* Get a token, and stuff it into RESULT.  The tokens are split
  1415.      exactly where the shell would split them. */
  1416.  get_token:
  1417.  
  1418.   /* Skip leading whitespace. */
  1419.   for (; string[i] && whitespace(string[i]); i++);
  1420.  
  1421.   start = i;
  1422.  
  1423.   if (!string[i] || string[i] == history_comment_char)
  1424.     return (result);
  1425.  
  1426.   if (member (string[i], "()\n")) {
  1427.     i++;
  1428.     goto got_token;
  1429.   }
  1430.  
  1431.   if (member (string[i], "<>;&|")) {
  1432.     int peek = string[i + 1];
  1433.  
  1434.     if (peek == string[i]) {
  1435.       if (peek ==  '<') {
  1436.     if (string[1 + 2] == '-')
  1437.       i++;
  1438.     i += 2;
  1439.     goto got_token;
  1440.       }
  1441.  
  1442.       if (member (peek, ">:&|")) {
  1443.     i += 2;
  1444.     goto got_token;
  1445.       }
  1446.     } else {
  1447.       if ((peek == '&' &&
  1448.       (string[i] == '>' || string[i] == '<')) ||
  1449.       ((peek == '>') &&
  1450.       (string[i] == '&'))) {
  1451.     i += 2;
  1452.     goto got_token;
  1453.       }
  1454.     }
  1455.     i++;
  1456.     goto got_token;
  1457.   }
  1458.  
  1459.   /* Get word from string + i; */
  1460.   {
  1461.     int delimiter = 0;
  1462.  
  1463.     if (member (string[i], "\"'`"))
  1464.       delimiter = string[i++];
  1465.  
  1466.     for (;string[i]; i++) {
  1467.  
  1468.       if (string[i] == '\\') {
  1469.  
  1470.     if (string[i + 1] == '\n') {
  1471.       i++;
  1472.       continue;
  1473.     } else {
  1474.       if (delimiter != '\'')
  1475.         if ((delimiter != '"') ||
  1476.         (member (string[i], slashify_in_quotes))) {
  1477.           i++;
  1478.           continue;
  1479.         }
  1480.     }
  1481.       }
  1482.  
  1483.       if (delimiter && string[i] == delimiter) {
  1484.     delimiter = 0;
  1485.     continue;
  1486.       }
  1487.  
  1488.       if (!delimiter && (member (string[i], " \t\n;&()|<>")))
  1489.     goto got_token;
  1490.  
  1491.       if (!delimiter && member (string[i], "\"'`")) {
  1492.     delimiter = string[i];
  1493.     continue;
  1494.       }
  1495.     }
  1496.     got_token:
  1497.  
  1498.     len = i - start;
  1499.     if (result_index + 2 >= size) {
  1500.       if (!size)
  1501.     result = (char **)xmalloc ((size = 10) * (sizeof (char *)));
  1502.       else
  1503.     result =
  1504.       (char **)xrealloc (result, ((size += 10) * (sizeof (char *))));
  1505.     }
  1506.     result[result_index] = (char *)xmalloc (1 + len);
  1507.     strncpy (result[result_index], string + start, len);
  1508.     result[result_index][len] = '\0';
  1509.     result_index++;
  1510.     result[result_index] = (char *)NULL;
  1511.   }
  1512.   if (string[i])
  1513.     goto get_token;
  1514.  
  1515.   return (result);
  1516. }
  1517.  
  1518. #if defined (STATIC_MALLOC)
  1519.  
  1520. /* **************************************************************** */
  1521. /*                                    */
  1522. /*            xmalloc and xrealloc ()                     */
  1523. /*                                    */
  1524. /* **************************************************************** */
  1525.  
  1526. static void memory_error_and_abort ();
  1527.  
  1528. static char *
  1529. xmalloc (bytes)
  1530.      int bytes;
  1531. {
  1532.   char *temp = (char *)malloc (bytes);
  1533.  
  1534.   if (!temp)
  1535.     memory_error_and_abort ();
  1536.   return (temp);
  1537. }
  1538.  
  1539. static char *
  1540. xrealloc (pointer, bytes)
  1541.      char *pointer;
  1542.      int bytes;
  1543. {
  1544.   char *temp;
  1545.  
  1546.   if (!pointer)
  1547.     temp = (char *)xmalloc (bytes);
  1548.   else
  1549.     temp = (char *)realloc (pointer, bytes);
  1550.  
  1551.   if (!temp)
  1552.     memory_error_and_abort ();
  1553.  
  1554.   return (temp);
  1555. }
  1556.  
  1557. static void
  1558. memory_error_and_abort ()
  1559. {
  1560.   fprintf (stderr, "history: Out of virtual memory!\n");
  1561.   abort ();
  1562. }
  1563. #endif /* STATIC_MALLOC */
  1564.  
  1565.  
  1566. /* **************************************************************** */
  1567. /*                                    */
  1568. /*                Test Code                */
  1569. /*                                    */
  1570. /* **************************************************************** */
  1571. #ifdef TEST
  1572. main ()
  1573. {
  1574.   char line[1024], *t;
  1575.   int done = 0;
  1576.  
  1577.   line[0] = 0;
  1578.  
  1579.   while (!done)
  1580.     {
  1581.       fprintf (stdout, "history%% ");
  1582.       t = gets (line);
  1583.  
  1584.       if (!t)
  1585.     strcpy (line, "quit");
  1586.  
  1587.       if (line[0])
  1588.     {
  1589.       char *expansion;
  1590.       int result;
  1591.  
  1592.       using_history ();
  1593.  
  1594.       result = history_expand (line, &expansion);
  1595.       strcpy (line, expansion);
  1596.       free (expansion);
  1597.       if (result)
  1598.         fprintf (stderr, "%s\n", line);
  1599.  
  1600.       if (result < 0)
  1601.         continue;
  1602.  
  1603.       add_history (line);
  1604.     }
  1605.  
  1606.       if (strcmp (line, "quit") == 0) done = 1;
  1607.       if (strcmp (line, "save") == 0) write_history (0);
  1608.       if (strcmp (line, "read") == 0) read_history (0);
  1609.       if (strcmp (line, "list") == 0)
  1610.     {
  1611.       register HIST_ENTRY **the_list = history_list ();
  1612.       register int i;
  1613.  
  1614.       if (the_list)
  1615.         for (i = 0; the_list[i]; i++)
  1616.           fprintf (stdout, "%d: %s\n", i + history_base, the_list[i]->line);
  1617.     }
  1618.       if (strncmp (line, "delete", strlen ("delete")) == 0)
  1619.     {
  1620.       int which;
  1621.       if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1)
  1622.         {
  1623.           HIST_ENTRY *entry = remove_history (which);
  1624.           if (!entry)
  1625.         fprintf (stderr, "No such entry %d\n", which);
  1626.           else
  1627.         {
  1628.           free (entry->line);
  1629.           free (entry);
  1630.         }
  1631.         }
  1632.       else
  1633.         {
  1634.           fprintf (stderr, "non-numeric arg given to `delete'\n");
  1635.         }
  1636.     }
  1637.     }
  1638. }
  1639.  
  1640. #endif                /* TEST */
  1641.  
  1642. /*
  1643. * Local variables:
  1644. * compile-command: "gcc -g -DTEST -o history history.c"
  1645. * end:
  1646. */
  1647.